package opf5.persistencia;

import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public abstract class OPF5SessionManager {
	private static SessionFactory factory = createFactory();
	
	public static Session getSession() {
		return factory.openSession();
	}

	private static SessionFactory createFactory() {
		Configuration config = createConfig();
		return config.buildSessionFactory();
	}

	private static Configuration createConfig() {
		Configuration config = new Configuration();
		
		registerMappers(config);
		
		config.setProperties(getHibernateProperties());

		return config;
	}

	private static Properties getHibernateProperties() {
		Properties props = new Properties();

		props.put("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
		
		props.put("hibernate.connection.url",
				"jdbc:sqlserver://localhost:1433; databaseName=OPF5DB");

		props.put("hibernate.connection.username", "opf5user");
		
		props.put("hibernate.connection.password", "opf5user");
		
		props.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
		
		return props;
	}
	
	private static void registerMappers(Configuration config) {
		config.addResource("opf5/persistencia/config/JugadorDBProxy.hbm.xml");
		config.addResource("opf5/persistencia/config/InfraccionDBProxy.hbm.xml");
		config.addResource("opf5/persistencia/config/PartidoDBProxy.hbm.xml");
		config.addResource("opf5/persistencia/config/InscripcionDBProxy.hbm.xml");
		config.addResource("opf5/persistencia/config/EquipoDBProxy.hbm.xml");
		config.addResource("opf5/persistencia/config/CalificacionDBProxy.hbm.xml");
		config.addResource("opf5/persistencia/config/PropuestaDBProxy.hbm.xml");
	}
	
	
	
}
